#include <iostream>
#include <cstdio>
#include <cassert>
#include <pthread.h>
#include <unistd.h>

using namespace std;

int g_val = 0;

// const std::ostream& fun()
// {
//     std::ostream os;
//     os << "我是一个独立的方法";
//     return os;
// }
std::string fun()
{
    return "我是一个独立的方法";
}

//新线程
void* thread_routine(void* args)
{
    const char *name = (const char*)args;
    while(true)
    {
        cout << "我是新线程,我正在运行! name: "  << name << " : " << fun() << " : " << g_val++ << " &g_val: " << &g_val << endl;
        sleep(1);
    }
}

int main()
{
    pthread_t tid;
    int n = pthread_create(&tid, nullptr, thread_routine, (void*)"thread one");
    assert(0 == n);
    (void)n;

    // 主线程
    while(true)
    {
        //地址->?
        char tidbuffer[64];
        snprintf(tidbuffer, sizeof(tidbuffer), "0x%x", tid);
        cout << "我是主线程,我正在运行!,我创建出来的线程的tid: " << tidbuffer << " : "
         << fun() << " : " << g_val << " &g_val: " << &g_val << endl;
        sleep(1);
    }

    return 0;
}